package com.mynetty.netty01;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class TimeClient {

	public static void connect(String host, int port){
		EventLoopGroup group = new NioEventLoopGroup();
		Bootstrap bootstrap = new Bootstrap();
		ChannelFuture future = null;
		
		bootstrap.group(group).channel(NioSocketChannel.class)
		         .option(ChannelOption.TCP_NODELAY, true)
		         .handler(new ChannelInitializer<SocketChannel>() {

					@Override
					protected void initChannel(SocketChannel socketChannel) throws Exception {
						socketChannel.pipeline().addLast(new TimeClientHandler());
					}
				});
		try {
			//发起异步请求
			future = bootstrap.connect(host, port).sync();
			//等待客户端链路关闭
			future.channel().closeFuture().sync();
			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}
	
	private static class TimeClientHandler extends ChannelHandlerAdapter{
		private final ByteBuf firstMessage;
		
		  public TimeClientHandler() {
	            byte[] req = "QUERY TIME ORDER ".getBytes();
	            firstMessage = Unpooled.buffer(req.length); // 开辟一个buffer
	            firstMessage.writeBytes(req);
	        }

	    @Override
		public void channelActive(ChannelHandlerContext ctx) throws Exception {
			ctx.writeAndFlush(firstMessage);
		} 
		  

		@Override
		public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
			ByteBuf buf = (ByteBuf) msg;  //将接收到的msg，转换成 byteBuf对象
			byte[] req = new byte[buf.readableBytes()];
			buf.readBytes(req);
			String body = new String(req, "UTF-8");
			System.out.println("TimeClient 接收到的消息：" + body);
			
			ctx.close(); //接受完消息关闭连接
		}
		
	  
		@Override
		public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
			System.out.println("释放资源：" + cause.getMessage()); //不重写将会看到堆栈信息以及资源无法关闭
			ctx.close();
		}
	}
	
	public static void main(String[] args){
		
		TimeClient.connect("127.0.0.1", 4040);
	}
	
	
}
